﻿namespace Leetcode.N0006;

public class Solution
{
    public string Convert(string s, int numRows)
    {
        var result = new char[s.Length];
        int step1;
        if (numRows == 1)
        {
            step1 = 1;
        }
        else
        {
            step1 = numRows + numRows - 2;
        }
        int rIndex = 0;

        for (int rowIndex = 0; rowIndex < numRows; rowIndex++)
        {
            int colIndex = 0;
            do
            {
                int sIndex;
                if (rowIndex == 0 || rowIndex == numRows - 1)
                {
                    sIndex = colIndex * step1 + rowIndex;
                }
                else
                {
                    if (colIndex % 2 == 0)
                    {
                        sIndex = (colIndex / 2) * step1 + rowIndex;
                    }
                    else
                    {
                        sIndex = (colIndex / 2) * step1 + numRows + (numRows - (rowIndex + 1) - 1);
                    }
                }

                if (sIndex >= s.Length)
                {
                    break;
                }
                result[rIndex] = s[sIndex];
                rIndex++;
                colIndex++;
            } while (true);

        }

        return new string(result);
    }
}
